﻿using CardMonitor.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CardMonitor.Pages
{
    /// <summary>
    /// ceshi.xaml 的交互逻辑
    /// </summary>
    public partial class ceshi : Page
    {
        public ceshi()
        {
            InitializeComponent();
          // this.ceshifangfa();
        }



            //public void ceshifangfa(string[] args)
            public void ceshifangfa()
            {
                // 1. 初始化读卡器
                using (var reader = new CardReader("COM8", 9600))
                {
                    try
                    {
                        // 2. 建立连接
                       // Console.WriteLine("正在连接读卡器...");
                        reader.Connect();

                        // 3. 发卡操作
                       // Console.WriteLine("\n=== 开始发卡流程 ===");
                        if (IssueCardOperation(reader))
                        {
                            Console.WriteLine("发卡成功！");

                            // 4. 写卡操作
                          //  Console.WriteLine("\n=== 开始写卡流程 ===");
                            WriteCardOperation(reader);
                        }
                    }
                    catch (Exception ex)
                    {
                      //  Console.WriteLine($"操作失败: {ex.Message}");
                    }
                }

               // Console.WriteLine("\n按任意键退出...");
                //Console.ReadKey();
            }

            /// <summary>
            /// 发卡操作示例
            /// </summary>
            static bool IssueCardOperation(CardReader reader)
            {
                try
                {
                    // 执行发卡命令
                   // Console.WriteLine("发送进卡指令...");
                    var enterCmd = reader.BuildCommand(0x34);
                    reader.SendCommand(enterCmd);

                 //   Console.WriteLine("移动卡片到出卡口...");
                    var moveCmd = reader.BuildCommand(0x35);
                    var response = reader.SendCommand(moveCmd);

                    // 验证响应
                    if (response[4] == 0x35 && response[5] == 0x00)
                    {
                       // Console.WriteLine("检测到卡片就绪");
                        return true;
                    }
                    return false;
                }
                catch (TimeoutException)
                {
                   // Console.WriteLine("错误：设备未响应");
                    return false;
                }
            }

            /// <summary>
            /// 写卡操作示例
            /// </summary>
            static void WriteCardOperation(CardReader reader)
            {
                try
                {
                    // 寻卡操作
                    Console.WriteLine("正在寻卡...");
                    var cardId = reader.RequestCard();
                    Console.WriteLine($"找到卡片: {BitConverter.ToString(cardId)}");

                    // 写入测试数据
                    var writeData = new byte[] { 0x01, 0x02, 0x03, 0x04 };

                    Console.WriteLine($"写入数据到块1: {BitConverter.ToString(writeData)}");
                    if (reader.WriteCardData(1, writeData))
                    {
                        Console.WriteLine("数据写入成功");
                    }
                    else
                    {
                        Console.WriteLine("写入验证失败");
                    }

                    // 追加写入其他数据
                    var moreData = new byte[] { 0xAA, 0xBB, 0xCC, 0xDD };
                    Console.WriteLine($"写入数据到块2: {BitConverter.ToString(moreData)}");
                    reader.WriteCardData(2, moreData);
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine($"参数错误: {ex.Message}");
                }
                catch (InvalidOperationException ex)
                {
                    Console.WriteLine($"操作异常: {ex.Message}");
                }
            }
        }
    }


